﻿using BeetleX.XRPC.Clients;
using BeetleX.XRPC.Packets;
using David.CanBus;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Win32;
using System.Diagnostics;
using System.Reflection;
using BeetleX.Clients;

namespace XaDevice
{
    using uint64_t = UInt64;
    using uint32_t = UInt32;
    using uint16_t = UInt16;
    using uint8_t = Byte;
    using int16_t = Int16;
    using int32_t = Int32;
    using CAN_BOOL = Byte;

    public  class XaDeviceClient
    {
        XRPCClient xrpcClient;
        IDeviceManager DeviceManager;
        IDevice Device;
        IMisc Misc;
        Version MatchingVersion=new Version(1,0,0,0);
        string AppDllName;

        public event NetworkCanDeviceEventNotify NetworkCanDeviceEventNotifyCallBack;
        public event NetworkCanDeviceHotPlug NetworkCanDeviceHotPlugCallBack;
        public int Init(string ip="127.0.0.1",int port=5557)
        {
            var actor = Process.GetCurrentProcess().ProcessName;
            var dllpath = Process.GetCurrentProcess().ProcessName;
            var dllModule = IntPtr.Zero;
            AppDllName = $"{actor.ToUpper()}";
            Debug.WriteLine($"actor:{actor}");
            xrpcClient = new XRPCClient(ip, port, 1);
            xrpcClient.TimeOut = 2000;
            xrpcClient.Options.ParameterFormater = new MsgPacket();
            DeviceManager = xrpcClient.Create<IDeviceManager>(actor);
            Device = xrpcClient.Create<IDevice>(actor);
            Misc = xrpcClient.Create<IMisc>(actor);
            Version serverVersion = null;
            serverVersion = Misc.GetXaDeviceServerVersion().Result;
            Misc.SetAppDllName(AppDllName);
            if (serverVersion.Major != MatchingVersion.Major || serverVersion.Minor != MatchingVersion.Minor)
            {
                throw new Exception("服务器和设备 通讯版本不匹配");
            }
            xrpcClient.AddDelegate<NetworkCanDeviceEventNotify>((dn, len, events) =>
            {
                return NetworkCanDeviceEventNotifyCallBack?.Invoke(dn, len, events);
            });
            xrpcClient.AddDelegate<NetworkCanDeviceHotPlug>((List<NetworkCanDevice> deviceInfos) =>
            {
                NetworkCanDeviceHotPlugCallBack?.Invoke(deviceInfos);
            });
            return 0;
        }
        public Task<List<NetworkCanDevice>> GetDevices()=> DeviceManager.GetDevices();
        public Task<bool> Open( string devicePath)=>Device.Open(AppDllName, devicePath);
        public Task<bool> Close( string devicePath)=>Device.Close(AppDllName, devicePath);
        public Task<bool> Configure(string devicePath, byte channelIndex, CanDeviceConfig_t config)=>Device.Configure(AppDllName,devicePath,channelIndex,config);
        public Task<CanDeviceConfig_t> GetConfigure(string devicePath, byte channelIndex) => Device.GetConfigure(AppDllName, devicePath,channelIndex);

        public Task<UInt32> SendCanXFrame(string devicePath, UInt32 count, CANX_Packet_t[] tx)=>Device.SendCanXFrame(AppDllName,devicePath,count,tx);

        public Task<RequestHardwareTimeStamp> GetTimeStamp( string devicePath)=>Device.GetTimeStamp(AppDllName,devicePath);
        public Task<bool> SetCanDeviceRunState(string devicePath, bool isStart)=>Device.SetCanDeviceRunState(AppDllName,devicePath,isStart);
    }

    



}
